﻿using System.Collections.Generic;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace Teele
{
    public partial class SettingsPage : PhoneApplicationPage
    {
        public SettingsPage()
        {
            InitializeComponent();

            List<AlertDistance> alertDistanceSource = new List<AlertDistance>();
            alertDistanceSource.Add(new AlertDistance() { Description = "50m", Distance = 50 });
            alertDistanceSource.Add(new AlertDistance() { Description = "100m", Distance = 100 });
            alertDistanceSource.Add(new AlertDistance() { Description = "250m", Distance = 250 });
            alertDistanceSource.Add(new AlertDistance() { Description = "500m", Distance = 500 });
            alertDistanceSource.Add(new AlertDistance() { Description = "750m", Distance = 750 });
            alertDistanceSource.Add(new AlertDistance() { Description = "1000m", Distance = 1000 });

            this.SpeedCamAlertPicker.ItemsSource = alertDistanceSource;

            for (int i = 0; i < alertDistanceSource.Count; i++)
            {
                if (alertDistanceSource[i].Distance == App.settings.SpeedCamAlertDistance)
                {
                    this.SpeedCamAlertPicker.SelectedIndex = i;
                    break;
                }
                else
                {
                    this.SpeedCamAlertPicker.SelectedIndex = 0;
                }
            }

            if (App.settings.DisableAutomaticIdle == true)
            {
                this.DisableIdleToggle.IsChecked = true;
            }
        }

        private void SpeedCamAlertPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0 && e.RemovedItems.Count > 0)
            {
                AlertDistance selectedDistance = (AlertDistance)e.AddedItems[0];
                App.settings.SpeedCamAlertDistance = selectedDistance.Distance;
            }
        }

        private void DisableIdleToggle_Unchecked(object sender, System.Windows.RoutedEventArgs e)
        {
            App.settings.DisableAutomaticIdle = false;
            PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled;
            this.DisableIdleToggle.Content = "Väljas";
        }

        private void DisableIdleToggle_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            App.settings.DisableAutomaticIdle = true;
            PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
            this.DisableIdleToggle.Content = "Sees";
        }
    }
}